package SHIYAN;

public class LinkedList {
    //成员变量
    private int  size;
    private Node first = null;   //head pointer
    private Node last = null ;   //tail pointer

    //成员内部类
    private class Node{
        private Object value;
        private Node   next;

        public Node(Object value) {
            this.value = value;
        }

        @Override
        public String toString() {
            return "Node [value=" + value + "]";
        }
    }

    //返回链表节点数量方法
    public int size() {

        return size;
    }

    //添加节点方法（Node）
    public void  addLast(Object value) {
        //构造一个新节点（node）
        Node node = new Node(value);

        if(first==null && last==null) {
            first = last = node;
        }
        else
        {
            last.next = node;
            //last指针向后移动一位
            last = last.next;
        }
        size++;
    }

    //获得第i个节点的内容
    public Object get(int index) {
        if(index>size|| index<0) {
            throw new IllegalArgumentException();
        }

        Node temp = this.first;
        for(int i=0; i<index; i++)
            temp = temp.next;
        return temp.value;
    }

    public static void main(String[] args) {

        LinkedList list = new LinkedList();

        list.addLast("Java");
        list.addLast("Basic");
        list.addLast("Python");
        System.out.println( list.get(1));
        System.out.println("size= " + list.size() );

    }
}

